<?php

require_once(dirname(__FILE__).'/Constants.php');
require_once(dirname(__FILE__).'/Config.php');
require_once(dirname(__FILE__).'/DB.php');
require_once(dirname(__FILE__).'/Util.php');
require_once(dirname(__FILE__).'/Statistics.php');

class Schedule {
    public $id;
    public $member_id;
    public $start_date;
    public $start_time;
    public $end_date;
    public $end_time;
    public $flag;
    
    public function Schedule ($id = '') {
        if (!$id) return;
        
        $dbh = DB::load_dbh();
        $result = mysql_query(sprintf('
            SELECT
                id,
                member_id,
                start_date,
                start_time,
                end_date,
                end_time,
                flag
            FROM
                schedules
            WHERE
                id = %d',
            mysql_real_escape_string($id)
        ));
        if (!$result) {
            trigger_error(mysql_error, E_USER_WARNING);
            return FALSE;
        }
        if ($row = mysql_fetch_assoc($result)) {
            $this->id = $row['id'];
            $this->member_id  = $row['member_id'];
            $this->start_date = $row['start_date'];
            $this->start_time = $row['start_time'];
            $this->end_date   = $row['end_date'];
            $this->end_time   = $row['end_time'];
            $this->flag       = $row['flag'];
        }
        return;
    }
    
    public function insert () {
        if (!$this || !$this->member_id) return FALSE;
        if (!isset($this->flag) || !is_numeric($this->flag)) $this->flag = 0;
        
        $dbh = DB::load_dbh();
        $result = mysql_query(sprintf("
            INSERT INTO
                schedules
            SET
                type       = 1,
                member_id  = '%d',
                start_date = '%s',
                start_time = '%s',
                end_date   = '%s',
                end_time   = '%s',
                flag       = '%d'",
            mysql_real_escape_string($this->member_id),
            mysql_real_escape_string($this->start_date),
            mysql_real_escape_string($this->start_time),
            mysql_real_escape_string($this->end_date),
            mysql_real_escape_string($this->end_time),
            mysql_real_escape_string($this->flag)
        ));
        if (!$result) {
            trigger_error(mysql_error, E_USER_WARNING);
            return FALSE;
        }
        $this->id = mysql_insert_id();
        
        $stat = new Statistics('schedules', date('Y-m-d'));
        $stat->increment('insert');
        
        return TRUE;
    }
    
    function update () {
        if (!$this || !$this->id || !$this->member_id) return FALSE;
        
        $dbh = DB::load_dbh();
        $result = mysql_query(sprintf("
            UPDATE
                schedules
            SET
                start_date = '%s',
                start_time = '%s',
                end_date   = '%s',
                end_time   = '%s',
                flag       = '%s'
            WHERE
                id  = '%d'",
            mysql_real_escape_string($this->start_date),
            mysql_real_escape_string($this->start_time),
            mysql_real_escape_string($this->end_date),
            mysql_real_escape_string($this->end_time),
            mysql_real_escape_string($this->flag),
            mysql_real_escape_string($this->id)
        ));
        
        if (!$result) {
            trigger_error(mysql_error, E_USER_WARNING);
            return FALSE;
        }
        
        $stat = new Statistics('schedules', date('Y-m-d'));
        $stat->increment('update');
        
        return mysql_affected_rows();
    }
    
    function delete () {
        if (!$this || !$this->member_id) return FALSE;
        
        $dbh = DB::load_dbh();
        $result = mysql_query(sprintf("
            DELETE FROM
                schedules
            WHERE
                id = '%d'
            LIMIT 1",
            mysql_real_escape_string($this->id)
        ));
        if (!$result) {
            trigger_error(mysql_error, E_USER_WARNING);
            return FALSE;
        }
        
        $stat = new Statistics('schedules', date('Y-m-d'));
        $stat->increment('delete');
        
        return mysql_affected_rows();
    }
    
    function get_list ($member_id, $start_date, $end_date) {
        if (!$member_id || !is_date($start_date) || !is_date($end_date)) return FALSE;
        if (is_array($member_id)) {
            foreach ($member_id as $id) {
                if (!is_numeric($id))
                    return FALSE;
            }
        }
        else if (!is_numeric($member_id))
            return FALSE;
        else
            $member_id = array($member_id);
        
        $in = join(', ', $member_id);
        $dbh = DB::load_dbh();
        $result = mysql_query(sprintf("
            SELECT
                id,
                member_id,
                start_date,
                start_time,
                end_date,
                end_time,
                flag
            FROM
                schedules
            WHERE
                member_id IN (%s) AND
                start_date <= '%s' AND end_date >= '%s' AND
                type = 1",
            $in,
            mysql_real_escape_string($end_date),
            mysql_real_escape_string($start_date)
        ));
        if (!$result) {
            trigger_error(mysql_error, E_USER_WARNING);
            return;
        }
        $schedules = array();
        while ($row = mysql_fetch_assoc($result))
            $schedules[] = $row;
        return $schedules;
    }

    function get_recent_list ($member_id, $start_date, $offset, $days) {
        if (!is_date($start_date) || !is_numeric($offset) || !is_numeric($days)) return FALSE;
        if (is_array($member_id)) {
            foreach ($member_id as $id) {
                if (!is_numeric($id))
                    return FALSE;
            }
        }
        else if (!is_numeric($member_id))
            return FALSE;
        else
            $member_id = array($member_id);

        $in = join(', ', $member_id);

        $dbh = DB::load_dbh();
        $query = sprintf("
            SELECT
                start_date
            FROM
                schedules
            WHERE
                member_id IN (%s) AND
                start_date >= '%s AND
                type = 1'
            GROUP BY
                start_date
            ORDER BY start_date
            LIMIT %d, %d",
            $in,
            mysql_real_escape_string($start_date),
            mysql_real_escape_string($offset),
            mysql_real_escape_string($days)
        );
        $result = mysql_query($query);
        if (!$result) {
            trigger_error(mysql_error, E_USER_WARNING);
            return;
        }

        $dates = array();
        while ($row = mysql_fetch_assoc($result)) {
            $dates[] = $row['start_date'];
        }

        $schedules = array();
        foreach ($dates as $date) {
            $schedule_per_date = Schedule::get_list($member_id, $date, $date);
            $schedules = array_merge($schedules, $schedule_per_date);
        }
        return $schedules;
    }

    function get_count ($member_id, $start_date, $end_date) {
        if (!is_numeric($member_id) || !is_date($start_date) || !is_date($end_date)) return FALSE;
        
        $dbh = DB::load_dbh();
        $result = mysql_query(sprintf("
            SELECT
                count(id) AS count
            FROM
                schedules
            WHERE
                member_id = %d AND
                start_date <= '%s' AND end_date >= '%s' AND
                type = 1",
            mysql_real_escape_string($member_id),
            mysql_real_escape_string($end_date),
            mysql_real_escape_string($start_date)
        ));
        if (!$result) {
            trigger_error(mysql_error, E_USER_WARNING);
            return;
        }
        if (!$row = mysql_fetch_assoc($result))
            return;
        $count = array(
            count => $row['count'],
        );
        return $count;
    }
    
    function get_count_total () {
        $dbh = DB::load_dbh();
        $result = mysql_query('
            SELECT
                MAX(id) AS count
            FROM
                schedules
            WHERE
                type = 1
        ');
        if (!$result) {
            trigger_error(mysql_error, E_USER_WARNING);
            return;
        }
        if ($row = mysql_fetch_assoc($result))
            return $row['count'];
        return;
    }
    
    function get_count_all () {
        $dbh = DB::load_dbh();
        $result = mysql_query('
            SELECT
                flag,
                COUNT(id) AS count
            FROM
                schedules
            WHERE
                type = 1
            GROUP BY
                flag
        ');
        if (!$result) {
            trigger_error(mysql_error, E_USER_WARNING);
            return;
        }
        $counts = array();
        while ($row = mysql_fetch_assoc($result))
            $counts[$row['flag']] = $row['count'];
        return $counts;
    }
    
    function get_count_daily ($target_date) {
        if (!is_date($target_date)) return;
        $dbh = DB::load_dbh();
        $result = mysql_query(sprintf("
            SELECT
                flag,
                COUNT(id) AS count
            FROM
                schedules
            WHERE
                start_date = '%s' AND
                type = 1
            GROUP BY
                flag",
            mysql_real_escape_string($target_date)
        ));
        if (!$result) {
            trigger_error(mysql_error, E_USER_WARNING);
            return;
        }
        $counts = array();
        while ($row = mysql_fetch_assoc($result))
            $counts[$row['flag']] = $row['count'];
        return $counts;
    }
}
